import * as Components from "../../game/entitas/component";
import * as Ecs from "framework/entitas/index"
import {GameEntity} from "./GameEntity";
import {GameMatcher} from "./GameMatcher";

export class <%=ContextName%>Context extends Ecs.Pool
{
<%
var components = contextData.comps
for (var i = 0; i < components.length; i++) {
    var c = components[i]
    if (!c.unique){
        continue
    }
%>
    public <%=c.name%> : Components.<%=c.Name%>
    public set<%=c.Name%>(val) : <%=ContextName%>Entity
    {
        if (this.has<%=c.Name%>) {
            throw new Ecs.SingleEntityException(GameMatcher.<%=c.Name%>);
        }
        let entity = this.createEntity('<%=c.name%>');
        entity.add<%=c.Name%>(val );
        this.<%=c.name%>Entity = this.set<%=c.Name%>(val);
        this.<%=c.name%>= this.<%=c.name%>Entity.<%=c.name%>
        return entity;
    }

    public replace<%=c.Name%>(val) :<%=ContextName%>Entity
    {
        let entity = this.<%=c.name%>Entity;
        if (entity == null) {
            this.set<%=c.Name%>(val);
        } else {
            entity.replace<%=c.Name%>(val );
        }
        this.<%=c.name%>= this.<%=c.name%>Entity.<%=c.name%>
        return entity;
    }

    public remove<%=c.Name%>()
    {
        this.destroyEntity(this.<%=c.name%>Entity);
        this.<%=c.name%>Entity = undefined
        this.<%=c.name%> = undefined
    }

    public get has<%=c.Name%>() : boolean
    {
        return this.<%=c.name%>Entity != undefined;
    }
<%}%>

    protected _newEntity(): <%=ContextName%>Entity {
        return new GameEntity()
    }

    public createEntity(name: string): <%=ContextName%>Entity {
        return super.createEntity(name) as <%=ContextName%>Entity;
    }

}
